//
//  MCImageObject.m
//  MClient
//
//  Created by  史正烨 on 10-8-25.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MCImageObject.h"

static MCImageObject *imgObjectInstance = nil;

@implementation MCImageObject

+ (MCImageObject *)instance {
	@synchronized(self) {
		if (imgObjectInstance == nil) {
			imgObjectInstance = [[self alloc] init]; // assignment not done here
		}
	}
	return imgObjectInstance;
}

- (id)init
{
	if (self = [super init]) {
        // Custom initialization
		imgObjects = [[NSMutableDictionary alloc] init];
    }
    return self;
}

- (UIImage*) getImage:(NSString*)aImgName withRetainFlag:(BOOL)bRetain
{
	UIImage* img = [imgObjects objectForKey:aImgName];
	if (nil == img) {
		img = [UIImage imageNamed:aImgName];
		
		if (nil == img) {
			@throw [NSException exceptionWithName:[NSString stringWithFormat:@"%@文件加载失败！", aImgName]
										   reason:nil userInfo:nil];
		}
		if (bRetain) {
			[imgObjects setValue:img forKey:aImgName];
		}
		
		return img;
	} else {
		return [img retain];
	}
}

- (void)dealloc {
	[imgObjects release];
    [super dealloc];
}

@end
